﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookExchange.Business;
using BookExchange.CustomClass.Base;
using BookExchange.ODataService;

namespace BookExchange
{
    public partial class NewPost : BasePage
    {
        RequestBusiness requestBus;
        CategoryBusiness categoryBus;
        List<Category> categoryList;

        public NewPost()
            : base()
        {
            requestBus = new RequestBusiness();
            categoryBus = new CategoryBusiness();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!this.IsUserAuthenticated) //always check this do not care postback or not
            {
                RedirectToLoginPage("Bạn cần đăng nhập mới sử dụng được chức năng này","createnew.aspx");

                return;
            }

            if (!IsPostBack)
            {
                LoadData();

                BindData();

            }
        }

        private void LoadData()
        {
            categoryList = categoryBus.GetCategoryList();
        }

        private void BindData()
        {
            lstCategory.DataSource = categoryList;
            lstCategory.DataTextField = "Description";
            lstCategory.DataValueField = "CategoryCode";
            lstCategory.DataBind();
        }

        protected void btnCreateNewPost_Click(object sender, EventArgs e)
        {
            //Get new Post from GUI
            var newPost = CreateNewRequestFromGUI();

            //Add to database
            requestBus.Insert(newPost);
            
            //Return to detail page to view the result
            RedirecToDetailPage(newPost.RequestCode);
            return;
        }

        private BookExchange.ODataService.Request CreateNewRequestFromGUI()
        {
            var newPost = new BookExchange.ODataService.Request();

            newPost.ID = Guid.NewGuid();
            newPost.Active = 0;
            newPost.BookName = GetString(txtBookName.Value, 20); //20 ky tu
            newPost.BookUrl = GetString(txtBookUrl.Value, 500);//500 ky tu
            newPost.CategoryCode = 1;
            newPost.Contact = GetString(txtContact.Value, 500);
            newPost.CreatingDate = DateTime.Now;
            newPost.LastModified = DateTime.Now;
            newPost.Description = txtDescription.Value;
            newPost.ExchangePlace = GetString(txtExchangePlace.Value, 500);//500 ky tu
            newPost.Finished = 0;
            newPost.MemberCode = CurrentMember.MemberCode;
            newPost.Price1 = decimal.Parse(txtPrice1.Value);
            newPost.Price2 = decimal.Parse(txtPrice2.Value);
            newPost.Quantity = 1;
            newPost.RequestCode = 1;
            newPost.RequestType = Request.QueryString["c"] == "r" ? RequestType.Request : RequestType.Post;
            newPost.Status = RequestStatus.S;

            return newPost;
        }

        private string GetString(IEnumerable<char> chars, int take)
        {
            return new string(chars.Take(take).ToArray());
        }
    }
}